﻿using System.Collections.Generic;

namespace BigfootWeb.Helpers
{
    /// <summary>
    /// This is a list item used when creating RadioList and Select elements
    /// </summary>
    public class SelectItem
    {

        /// <summary>
        /// Determines if this item will be selected in the list
        /// </summary>
        public bool Selected;
        
        public SelectItem()
        {
        }

        /// <summary>
        /// Specify the text when creating. Will assign the text to the value and text element
        /// </summary>
        /// <param name="Text">The text to use for the option element. Assigned to the value by default as well</param>
        public SelectItem(string Text)
        {
            Value = Text;
            this.Text = Text;
        }

        /// <summary>
        /// Specify the text and value for the option tag
        /// </summary>
        /// <param name="Text">The text to use for the option element.</param>
        /// <param name="Value">The value to use for the option element.</param>
        public SelectItem(string Text, string Value)
        {
            this.Value = Value;
            this.Text = Text;
        }

        /// <summary>
        /// Specify the text and value for the option tag as well as wether it is selected
        /// </summary>
        /// <param name="Text">The text to use for the option element.</param>
        /// <param name="Value">The value to use for the option element.</param>
        /// <param name="Selected">Indictes weather the current item is selected or not</param>
        public SelectItem(string Text, string Value, bool Selected)
        {
            this.Value = Value;
            this.Text = Text;
            this.Selected = Selected;
        }
        
        private string _value;
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
        
        private string _text;
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                if (string.IsNullOrEmpty(_value))
                {
                    _value = _text;
                }
            }
        }

    }

    /// <summary>
    /// This is SelectItem list builder. It aids in the fluid creation of a collection of SelectItems
    /// </summary>
    public class SelectItemList
    {

        private List<SelectItem> _items = new List<SelectItem>();

        /// <summary>
        /// Adds a list item where the text and value is the same
        /// </summary>
        /// <param name="text">Text to use for the list item. Will be used for the value as well</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string text)
        {
            _items.Add(new SelectItem(text));
            return this;
        }

        /// <summary>
        /// Adds a list item where the text and value might be different
        /// </summary>
        /// <param name="value">The value to assign to the list item</param>
        /// <param name="text">Text to use for the list item.</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string value, string text)
        {
            _items.Add(new SelectItem(text, value));
            return this;
        }

        /// <summary>
        /// Adds a list item where the text and value might be different and you can specify wether the item is selected
        /// </summary>
        /// <param name="value">The value to assign to the list item</param>
        /// <param name="text">Text to use for the list item.</param>
        /// <param name="selected">Determines if the item is selected</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string value, string text, bool selected)
        {
            _items.Add(new SelectItem(text, value, selected));
            return this;
        }

        /// <summary>
        /// Adds a list of ListItems where the text and value is the same using an array of texts
        /// </summary>
        /// <param name="itemtexts">Array of texts to use when creating the List items. Will be used for the value as well</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string[] itemtexts)
        {
            foreach (var t in itemtexts)
            {
                _items.Add(new SelectItem(t));
            }
            return this;
        }

        /// <summary>
        /// Returns the created list of items
        /// </summary>
        public List<SelectItem> ToList()
        {
            return _items;
        }

        /// <summary>
        /// Returns an array of SelectItem
        /// </summary>
        public SelectItem[] ToArray()
        {
            return _items.ToArray();
        }

    }
}
